<?php 
/**
 * APP管理
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\MealCardOrder;
use App\Models\System;
use App\Services\OSS;

class MealAppController extends Controller
{
	/**
	 * 首页内容编辑
	 */
	public function homePageEdit(Request $request)
	{
		$content_m = System::where('option','user_agreement')->first();
		$content = json_decode(file_get_contents(config('app.homejson')."ppt.json"),true);
		return view('appmanage.editing',['content'=>$content[0],'user_agr'=>$content_m->content]);
	}

	/**
	 * 添加今日头条内容
	 */
	public function todayNewsAdd(Request $request)
	{

		$post = $request->only(['toutiaocontent']);

		if(empty($post['toutiaocontent'])){
			$post['toutiaocontent'] = [];
		}
		
		$con = [];

		foreach ($post['toutiaocontent'] as $k => $v) {
			if(!empty($v)){
				$con[] = $v;
			}
		}

		$content = json_decode(file_get_contents(config('app.homejson')."ppt.json"),true);

		$content[0]['toutiao'] = $con;

        $fileName = 'ppt.json';

        $filePath = 'APP/HomeJson/';

        $length = file_put_contents(public_path('tmp/ppt.json'), json_encode($content));

        OSS::publicUpload(config('aliyun.BucketName'),$filePath.$fileName,public_path('tmp/ppt.json'));

        @unlink(public_path('tmp/ppt.json'));

		if($length>0){
			return response()->json(['success'=>true,'code'=>'0','message'=>'修改成功！']);
		}else{
			return response()->json(['success'=>false,'code'=>'1','message'=>'修改失败，请稍后重试！']);
		}

	}

	/**
	 * 轮播图添加修改 broadcast
	 */
	public function broadcastEdit(Request $request)
	{
		$post = $request->only([
			'shop_id',
			'broadcast_id',
			'shop_tel',
			'shop_name',
			'targettype',
			'action',//1添加 0修改
		]);
		$action_de = [0,1];
		$targettype = ['url','shop','show'];
		$targettype_de = ['url'=>2,'shop'=>1,'show'=>3];
		if(!in_array($post['action'],$action_de)||!in_array($post['targettype'], $targettype)){
			return response()->json(['success'=>false,'code'=>'1','message'=>'未识别您的操作！']);
		}
		if($request->hasFile('lbt')){
			$lbt = $request->file('lbt');
			$filename = md5(uniqid(microtime(true),true)).'.png';
			$filePath = 'BroadcastImg/';
			OSS::publicUpload(config('aliyun.BucketName'),$filePath.$filename,$lbt);
			$filename = 'http://jiashilexiang.oss-cn-beijing.aliyuncs.com/BroadcastImg/'.$filename;
		}else{
			if($post['action']==1){
				return response()->json(['success'=>false,'code'=>'2','message'=>'请上传图片后提交！','data'=>$content]);
			}
		}

		$content = json_decode(file_get_contents(config('app.homejson')."ppt.json"),true);
		$lunbo = $content[0]['lunbo'];
		$lbtid = $content[0]['lbtid'] ?? 0;
		if($post['action']==0){
			if($post['targettype']=='url'){
				$newk = [
					'id'=>$post['broadcast_id'],
					'filePath'=>'',
					'shop_id'=>'',
					'url'=>$post['shop_tel'],
					'shop_name'=>'',
					'type'=>$targettype_de[$post['targettype']]
				];
			}else if($post['targettype']=='shop'){
				$newk = [
					'id'=>$post['broadcast_id'],
					'filePath'=>'',
					'shop_id'=>$post['shop_id'],
					'url'=>'',
					'shop_name'=>$post['shop_name'],
					'type'=>$targettype_de[$post['targettype']]
				];
			}else if($post['targettype']=='show'){
				$newk = [
					'id'=>$post['broadcast_id'],
					'filePath'=>'',
					'shop_id'=>'',
					'url'=>'',
					'shop_name'=>'',
					'type'=>$targettype_de[$post['targettype']]
				];
			}
			foreach ($lunbo as $key => $value) {
				if($value['id']==$post['broadcast_id']){
					if(!isset($filename)){
						$newk['filePath'] = $value['filePath'];
					}else{
						$newk['filePath'] = $filename;
					}
					$lunbo[$key] = $newk;
				}
			}
		}else if($post['action']==1){
			$newlbtid = $lbtid+1;
			if($post['targettype']=='url'){
				$lunbo[] = [
					'id'=>$newlbtid,
					'filePath'=>$filename,
					'shop_id'=>'',
					'url'=>$post['shop_tel'],
					'shop_name'=>'',
					'type'=>$targettype_de[$post['targettype']],
				];
			}else if($post['targettype']=='shop'){
				$lunbo[] = [
					'id'=>$newlbtid,
					'filePath'=>$filename,
					'shop_id'=>$post['shop_id'],
					'url'=>'',
					'shop_name'=>$post['shop_name'],
					'type'=>$targettype_de[$post['targettype']],
				];
			}else if($post['targettype']=='show'){
				$lunbo[] = [
					'id'=>$newlbtid,
					'filePath'=>$filename,
					'shop_id'=>'',
					'url'=>'',
					'shop_name'=>'',
					'type'=>$targettype_de[$post['targettype']],
				];
			}
			
			$content[0]['lbtid'] = $newlbtid;
		}
		$content[0]['lunbo'] = $lunbo;

		$fileName = 'ppt.json';

        $filePath = 'APP/HomeJson/';

        $length = file_put_contents(public_path('tmp/ppt.json'), json_encode($content));

        OSS::publicUpload(config('aliyun.BucketName'),$filePath.$fileName,public_path('tmp/ppt.json'));

        @unlink(public_path('tmp/ppt.json'));

		if($length>0){
			return response()->json(['success'=>true,'code'=>'0','message'=>'修改成功！']);
		}else{
			return response()->json(['success'=>false,'code'=>'1','message'=>'修改失败，请稍后重试！']);
		}
		
	}

	/**
	 * 删除轮播图
	 */
	public function broadcastDel(Request $request)
	{
		$post = $request->only([
			'broadcast_id',
		]);
		if(empty($post['broadcast_id'])){
			return reponse()->json(['success'=>false,'code'=>'1','message'=>'无法删除！']);
		}
		$content = json_decode(file_get_contents(config('app.homejson')."ppt.json"),true);

		$lunbo = $content[0]['lunbo'];

		$newlunbo = [];

		foreach ($lunbo as $key => $value) {
			if($value['id']!=$post['broadcast_id']){
				$newlunbo[] = $value;
			}
		}

		$content[0]['lunbo'] = $newlunbo;

		$fileName = 'ppt.json';

        $filePath = 'APP/HomeJson/';
        
		$length = file_put_contents(public_path('tmp/ppt.json'), json_encode($content));

		OSS::publicUpload(config('aliyun.BucketName'),$filePath.$fileName,public_path('tmp/ppt.json'));

        @unlink(public_path('tmp/ppt.json'));

		if($length>0){
			return response()->json(['success'=>true,'code'=>'0','message'=>'已删除。']);
		}else{
			return response()->json(['success'=>false,'code'=>'2','message'=>'删除失败，请稍后重试！']);
		}
	}

	/**
	 * 小程序默认饭卡
	 */
	public function defaultCardImg(Request $request)
	{
		if(!$request->hasFile('fanka')){
			return response()->json([
				'success'=>false,
				'code'=>'003',
				'message'=>'没有上传文件'
			]);
		}
		$fanka = $request->file('fanka');
		$content = json_decode(file_get_contents(config('app.homejson')."ppt.json"),true);
		// $cardImg = $content[0]['card_default_img'];
		$filename = md5(uniqid(microtime(true),true)).'.png';
		$filePath = 'BroadcastImg/';
		OSS::publicUpload(config('aliyun.BucketName'),$filePath.$filename,$fanka);
		$filename = 'http://jiashilexiang.oss-cn-beijing.aliyuncs.com/BroadcastImg/'.$filename;
		$content[0]['card_default_img'] = $filename;
		$fileName = 'ppt.json';
		$filePath = 'APP/HomeJson/';
		$length = file_put_contents(public_path('tmp/ppt.json'), json_encode($content));
		OSS::publicUpload(config('aliyun.BucketName'),$filePath .$fileName,public_path('tmp/ppt.json'));
		@unlink(public_path('tmp/ppt.json'));
		if($length>0){
			return response()->json(['success'=>true,'code'=>'0','message'=>'上传成功']);
		}else{
			return response()->json(['success'=>false,'code'=>'2','message'=>'修改失败，请稍后重试！']);
		}
	}

	/**
     * 版本发布
     */
    public function versionRelease(Request $request)
    {

        $post = $request->only(['version']);

        Redis::set('lexiang_shop_version',$post['version']);

        return response()->json(['success'=>true,'message'=>'发布成功','code'=>'000']);

    }
}